package com.hm.oltu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hm.common.util.CommonUtil;
import com.hm.oltu.auto.OltuProperties;
import com.hm.oltu.service.ClientService;
import com.hm.oltu.service.OAuthService;
import com.hm.oltu.util.RedisUtil;

@Service
public class OAuthServiceImpl implements OAuthService {

	@Autowired
	private ClientService clientService;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private OltuProperties oltuProperties;

	@Override
	public void addAuthCode(String authCode, String username) {
		redisUtil.set(authCode, username, oltuProperties.getExpireTime());
	}

	@Override
	public void addAccessToken(String accessToken, String username) {
		redisUtil.set(accessToken, username, oltuProperties.getExpireTime());
	}

	@Override
	public boolean checkAuthCode(String authCode) {
		return CommonUtil.isNotEmpty(redisUtil.get(authCode));
	}

	@Override
	public boolean checkAccessToken(String accessToken) {
		return CommonUtil.isNotEmpty(redisUtil.get(accessToken));
	}

	@Override
	public String getUsernameByAuthCode(String authCode) {
		return (String) redisUtil.get(authCode);
	}

	@Override
	public String getUsernameByAccessToken(String accessToken) {
		return (String) redisUtil.get(accessToken);
	}

	@Override
	public boolean checkClientId(String clientId) {
		return clientService.findByClientId(clientId) != null;
	}

	@Override
	public boolean checkClientSecret(String clientSecret) {
		return clientService.findByClientSecret(clientSecret) != null;
	}

	@Override
	public long getExpireIn() {
		return 3600;
	}

}
